<template>
  <div>
    <el-breadcrumb class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/main' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'adminJobFair'}">招聘会中心</el-breadcrumb-item>
      <el-breadcrumb-item >新增招聘会</el-breadcrumb-item>
    </el-breadcrumb>

    <el-form  label-width="100px" class="demo-ruleForm"
             :model="jobFair"  ref="jobFair" style="margin-top: 30px"
    >
      <table class="btm" v-loading="loading">
        <el-row>
          <el-col :span="14">
            <el-form-item label="招聘会名称" prop="resource">
              <el-input v-model="jobFair.jobfairName" placeholder="如“XXX招聘会”" style="width: 350px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="发布人" prop="resource">
              <el-input v-model="jobFair.createPerson" style="width: 160px" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="举办时间" prop="resource">
              <el-date-picker
                      v-model="startAndStopTime"
                      type="daterange"
                      @change="changeTime"
                      :unlink-panels=true
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="举办地址" prop="resource">
              <el-input v-model="jobFair.address" style="width: 350px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="图片" >
              <el-upload
                      class="avatar-uploader"
                      :action="$config.url+'/jobfair/headerImg'"
                      :show-file-list="false"
                      :on-success="handleAvatarSuccess"
                      :before-upload="beforeAvatarUpload"
              >
                <img v-if="jobFair.jobfairImg" :src="$config.url+jobFair.jobfairImg" class="avatar" style="height: 190px;width: 190px">
                <div v-else><img src="../img/noImg.gif"
                                 style="height: 190px;width: 190px"></div>
              </el-upload>

            </el-form-item>
          </el-col>
        </el-row>
      </table>
      <el-form-item>
        <el-button type="primary" @click="insertJobFair">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  export default {
    name: "admin-job-fair-add",
    data(){
      return {
        startAndStopTime:'',
        jobFair:{
          jobfairName:'',
          createPerson:'',
          startTime:'',
          stopTime:'',
          address:'',
          jobfairImg:''
        }
      }
    },
    created(){
      this.jobFair.createPerson=this.$store.state.admin.name;
    },

    methods:{
      insertJobFair(){
        this.$ajax.post(this.$config.url + `jobfair/addJobFair`,this.jobFair
        ).then(res => {
          if(res.data.message=="succ"){
            this.$message.success("增加成功");
            this.$router.push("adminJobFair");
          }else{
            this.$message.error("出错了，添加失败")
          }
        }).finally(() => {
        })
      },
      changeTime(val){
        if(val){
          this.jobFair.startTime=val[0];
          this.jobFair.stopTime=val[1];
        }else{
          this.jobFair.startTime='';
          this.jobFair.stopTime='';
        }
      },
      handleAvatarSuccess(res) {
        this.jobFair.jobfairImg =res;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isPNG = file.type === 'image/png';
        const isGIF = file.type === 'image/gif';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG && !isPNG && !isGIF) {
          this.$message.error('上传头像图片只能是 JPG 或PNG或gif格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
      },

    },

  }
</script>

<style scoped>

</style>